window.index_monitor_status_get = function (monitorId ,type ,nameAttr){
	var el_system_health = $('[data-type=system-health]')
	el_system_health.html("")
	var el_system_health_title = $('<div class="system-health-title"></div>').appendTo(el_system_health)
	var el_system_health_body = 
		$('<div class="monitor-panel scale-panel" style="height:100px" id="scale_list"></div>').appendTo(el_system_health)
	var buffer = [];
	buffer.push('<span class="dib ml5 mr5 bggreen" style="width: 30px;height: 10px;"></span>正常');
	buffer.push('<span class="dib ml5 mr5 bgorange" style="width: 30px;height: 10px;"></span>性能');
	buffer.push('<span class="dib ml5 mr5 bgred" style="width: 30px;height: 10px;"></span>故障');
	buffer.push('<span class="dib ml5 mr5 bgdeepblue" style="width: 30px;height: 10px;"></span>凭证');
	buffer.push('<span class="dib ml5 mr5 bggrey" style="width: 30px;height: 10px;"></span>未知');
	el_system_health_title.append(buffer.join(""));

	um_ajax_get({
        url : "monitorView/queryMonitorStatus",
        paramObj : {monitorId : monitorId,type : type,direction : 0,
                    time:$("#query_time_label").text()},
        isLoad : true,
        successCallBack : function (data){
            //$("#scale_list").height(data.timerTable.length * 75);
            $("#scale_list").html("");
            for (var i = 0; i < data.timerTable.length; i++)
            {
                var el = $('<div class="scale" style="padding-top:10px"></div>');
                $("#scale_list").append(el);
                g_monitor.monitorStatusChart(el ,{
                    time : $("#query_time_label").text(),
                    data : data.timerTable[i].status,
                    name : data.timerTable[i][nameAttr]
                });
            }
            $("[data-type=monitor-head-ip]").text("(" + data.ipvAddress + ")");
            var el_event_li = $("[class*=monitor-event]").parent();
            if (data.total == 0)
            {
            	el_event_li.find("[class=icon-warning-sign]").remove();
            }
            else
            {
            	if (el_event_li.find("[class=icon-warning-sign]").size() == 0)
            	{
            		el_event_li.append('<i class="icon-warning-sign" style="margin-left:45px"></i>');
            	}
            }
            
            g_dialog.waitingAlertHide($("#monitor_info_detail"));
        }
    });
}

$(document).ready(function (){
require(['monitorEvent',
		'event','monitorInfo'] ,function (event ,pevent ,monitorInfo){

var urlParamObj = index_query_param_get();

var index_monitorId = urlParamObj.monitorId

var index_monitorType

var index_monitorNameAttr = "monitorName"

var index_is_oracle_or_db2 = false

var index_hostMonitorId

$("#query_time_label").text(g_moment().format("YYYY-MM-DD HH:mm:ss"));

event_init()

monitor_info_query()

function event_init()
{
	$("#timeinphidden").jeDate({
	    format: 'YYYY-MM-DD hh:mm:ss',
	    isTime:true,
	    choosefun:function(elem, val) {
			$("#query_time_label").text(val);
			monitor_refresh()
	    },
	    okfun:function(elem, val) {
			$("#query_time_label").text(val);
			monitor_refresh()
	    }
	});

	$("#monitor_tab_s").find("li").click(function (){
		$(this).siblings().removeClass("active")
		$(this).addClass("active")

		$(".monitor-tab-div").hide()
		$(".monitor-tab-div").eq($(this).index()).show()

		g_grid.resize($("#event_table_1"))
		//g_grid.resize($("#event_table_2"))
		//g_grid.resize($("#event_table_3"))
	})

	$("#run_btn_i").click(function (){
	})

	$("#refresh_btn_i").click(function (){
		monitor_refresh()
	})
}

function monitor_refresh(){
	//$(".monitor-tab").find("li").eq(0).click()
	// index_monitor_status_get(index_monitorId ,index_monitorType ,index_monitorNameAttr)
	// index_monitor_init();
	// event_list_init()

	if (urlParamObj.queryByAsset == 1){
		$("#monitor_tab_s").find("li").removeClass("active")
		$("#monitor_tab_s").find("li").eq(0).addClass("active")
		if ($("#monitor_tab").find("li[class=active]").attr("type") == "monitorInfo")
			monitor_info_render(index_monitorId)
		else
			asset_info_render()
	}
	else
	{
		$("#monitor_tab_s").find("li").eq(0).click()
		index_monitor_status_get(index_monitorId ,index_monitorType ,index_monitorNameAttr)
		index_monitor_init();
		event_list_init()
	}

	
}

function monitor_info_query()
{
	if (urlParamObj.queryByAsset == 1)
	{
		var el_monitor_tab = $("#monitor_tab")
		um_ajax_get({
			url : "monitorView/queryMonitorForEd",
			paramObj : {edId : urlParamObj.assetId},
			successCallBack : function (data){
				var tmpData = _.find(data ,function (tmp){
					return tmp.isHostMonitor == "1"
				})
				if (tmpData)
					urlParamObj.hostMonitorId = tmpData.monitorId
				monitor_tab_render(el_monitor_tab ,data)
				//monitor_info_render(data[0].monitorId)
				asset_info_render()
			}
		})
	}
	else if (urlParamObj.queryByMonitor == 1)
	{
		monitor_info_render(index_monitorId)
	}
}

function monitor_tab_render(el_monitor_tab ,data){
	var aData
	var el_monitor_ul = el_monitor_tab.find("ul")
	var active
	el_monitor_ul.append('<li type="assetInfo" class="active">资产信息</li>')
	for (var i = 0; i < data.length; i++) {
		aData = data[i]
		//i == 0 ? (active = "active") : (active = "")
		el_monitor_ul.append('<li class="" data-type="'+aData.monitorId+'" type="monitorInfo">'+aData.monitorName+'</li>')
	}
	el_monitor_ul.find("li").click(function (){
		$(this).siblings().removeClass("active")
		$(this).addClass("active")
		$("#monitor_tab_s").find("li").removeClass("active")
		$("#monitor_tab_s").find("li").eq(0).addClass("active")
		$(".monitor-head-name").find("div").hide()
		$(".monitor-head-name").children().text("")
		if ($(this).attr("type") == "monitorInfo")
			monitor_info_render($(this).attr("data-type"))
		if ($(this).attr("type") == "assetInfo")
			asset_info_render()
	})
}

function monitor_info_render(tmpMonitorId){
	$("#monitor_info").empty()
	um_ajax_get({
		url : "/monitorView/queryMonitorInfoByMonitorId",
		paramObj : {monitorId : tmpMonitorId},
		successCallBack : function (aData){
			var tmp_monitor_type = aData.monitorType
			if (aData.version == "LINUX_SNMP")
			{
				tmp_monitor_type = "LINUX_SNMP"
			}
			if (aData.version == "WINDOWS_SNMP")
			{
				tmp_monitor_type = "WINDOWS_SNMP"
			}
			if (aData.version == "ORACLE" || aData.version == "DB2")
			{
				index_is_oracle_or_db2 = true
			}
			var el_monitor_head_name = $(".monitor-head-name")
			el_monitor_head_name.find("div").prop('class','')
			el_monitor_head_name.find("div").addClass(tmp_monitor_type)
			el_monitor_head_name.find("p").text(aData.monitorName)
			$(".monitor-head-name").find("div").show()

			index_monitorId = tmpMonitorId

			index_monitorType = tmp_monitor_type

			$("[data-id=monitorId]").val(tmpMonitorId)
			$("[data-id=param-monitorTypeId]").val(tmp_monitor_type)
			$("[data-id=param-assetId]").val(aData.edId)

			index_monitor_status_get(tmpMonitorId ,tmp_monitor_type ,"monitorName")

			$.ajax({
				type: "GET",
				url: "tpl/monitor_classify/" + tmp_monitor_type.toLocaleLowerCase() + ".html",
				success :function(data)
				{
					$("#monitor_info").html(data)
					event_list_init()
				}
			});
		}
	})
}

function event_list_init(){
	var el_event = $("[data-type=event]")
	$.ajax({
			type: "GET",
			url: "module/monitor_obj/monitor_tpl.html",
			success :function(data)
			{
				el_event.html($(data).find("[id=monitor_event_list_template]").html())
				event_list_get()
				$("#event_ignore_btn").click(function (){
					if ($(this).attr("data-type") == "fault")
					{
						pevent.ignore({
							gridEl : $("#event_table_1"),
							attr : "faultNO",
							ignore_url : "faultAlarmEvent/doIgnore",
							cb: function() {
								g_grid.refresh($("#event_table_1"))
							}
						});
					}
					else
					{
						pevent.ignore({
							gridEl : $("#event_table_1"),
							attr : "performanceNo",
							ignore_url : "performanceEvent/doPerformanceEventIgnore",
							cb: function() {
								g_grid.refresh($("#event_table_1"))
							}
						});
					}
				})
			}
		});
}

function event_list_get()
{
	__event_list_get($("#event_info_div_1") ,$("#event_table_1") ,"1")
	//__event_list_get($("#event_info_div_2") ,$("#event_table_2") ,"3")
	//__event_list_get($("#event_info_div_3") ,$("#event_table_3") ,"2,4")

	$("#event_ignore_btn").attr("data-type" ,"fault")

	function __event_list_get(el_event_info_tmp ,el_grid_tmp ,status){
		var el_ul_tmp = el_event_info_tmp.find("ul")
		var urlParamObjTmp = $.extend({} ,index_query_param_get());
		var param_tmp = {param : urlParamObjTmp ,elGrid : el_grid_tmp}
		param_tmp.param.faultStatus = status
		param_tmp.param.perfStatus = status
		param_tmp.param.flag = 1
		if (index_is_oracle_or_db2)
			param_tmp.param.monitorId = param_tmp.param.monitorId + "_"
		el_ul_tmp.find("li").unbind("click")
		el_ul_tmp.find("li").click(function (){
			$(this).siblings().removeClass("active")
			$(this).addClass("active")
			if ($(this).attr("data-action") == "fault_event_list"){
				event.fault_event_list(param_tmp)
				$("#event_ignore_btn").attr("data-type" ,"fault")
			}	
			else{
				event.perform_event_list(param_tmp)
				$("#event_ignore_btn").attr("data-type" ,"perform")
			}
		})
		event.fault_event_list(param_tmp)
	}
}

function asset_info_render(){
	$("#monitor_info").empty()
	$("[data-type=system-health]").empty()
	$("[data-id=monitorId]").val(urlParamObj.hostMonitorId)
	urlParamObj.chooseEndDate = $("#query_time_label").text()
	urlParamObj.time = $("#query_time_label").text()
	urlParamObj.monitorId = urlParamObj.hostMonitorId
	$.ajax({
		type: "GET",
		url: "tpl/monitor_classify/asset_info.html",
		success :function(data)
		{
			$("#monitor_info").html(data)
			um_ajax_get({
				url : "AssetOperation/queryAssetDetail",
				paramObj : {edId : urlParamObj.assetId ,mainIp : urlParamObj.mainIp},
				successCallBack : function (data){
					$("#asset_base_info_div").umDataBind("render" ,data.assetStore)
				}
			})
			if (urlParamObj.hostMonitorId && urlParamObj.hostMonitorId != "null"){
				monitorInfo.cpu_memo_use_rate({
			        url : "monitorview/os/windows/queryAllUsageInfoByChooseTime",
			        paramObj : urlParamObj,
			        line_chart : $("#cpu_use_line_chart"),
			        type:"cpu"
			    })
			    monitorInfo.cpu_memo_use_rate({
			        url : "monitorview/os/windows/queryAllUsageInfoByChooseTime",
			        paramObj : urlParamObj,
			        line_chart : $("#memory_use_line_chart"),
			        type:"memory"
			    })
			    monitorInfo.disk_used_chart($("#disk_use_chart") ,{
			        paramObj : urlParamObj
			    })
			}
			else
			{
				$("#cpu_use_line_chart").closest(".box-item").remove()
				$("#memory_use_line_chart").closest(".box-item").remove()
				$("#disk_use_chart").closest(".m-panel").remove()
			}
			event_list_init()
		}
	});
}

})
});